// pages/detail/detail.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pid: null, //商品id
        detailData: null, //详情数据
        temData: null, //温度
        sugarData: null, //糖度
        descData: null, //内容详情
        creamData: null, //奶油
        num: 1, //商品数量
        tindex: 0,
        sindex: 0,
        cindex: 0,
        sidData: null, //存储sid
        orderData: null, //订单数据
        love: false //收藏状态 默认false
    },
    // 商品详情数据
    onLoad(options) {
        console.log(options)
        this.setData({
            pid: options.pid
        })
        let temData = this.data.temData
        wx.request({
            url: 'http://www.kangliuyong.com:10002/productDetail',
            method: 'GET',
            data: {
                appkey: app.globalData.appkey,
                pid: this.data.pid
            },
            success: (res) => {
                console.log(res);
                if (res.data.result[0].type_desc == '瑞纳冰' && res.data.result[0].tem == '') {
                    temData = '冰'
                } else {
                    temData = res.data.result[0].tem.split("/")
                }
                this.setData({
                    detailData: res.data.result[0],
                    temData: temData,
                    sugarData: res.data.result[0].sugar.split("/"),
                    creamData: res.data.result[0].cream.split("/"),
                    descData: res.data.result[0].desc.split("\n"),
                })
            }
        })
    },
    onShow() {
        console.log(app.globalData.token);
        if (app.globalData.token !== '') {
            wx.request({
                url: 'http://www.kangliuyong.com:10002/findAllLike',
                data: {
                    appkey: app.globalData.appkey,
                    tokenString: app.globalData.token
                },
                success: res => {
                    console.log(res);
                    let arr = res.data.result
                    console.log(arr);
                    for (let i = 0; i < arr.length; i++) {
                        // 判断pid是否相等 相等则收藏
                        if (this.data.pid == arr[i].pid) {
                            this.setData({
                                love: true
                            })
                        }
                    }
                }
            })
        }
    },
    // 改变温度index
    changeTindex(e) {
        let tindex = this.data.tindex
        this.setData({
            tindex: e.currentTarget.dataset.index,
        })
    },
    // 改变糖度index
    changeSindex(e) {
        let sindex = this.data.sindex
        this.setData({
            sindex: e.currentTarget.dataset.index,
        })
    },
    // 改变奶油index
    changeCindex(e) {
        let cindex = this.data.cindex
        this.setData({
            cindex: e.currentTarget.dataset.index
        })
    },
    // 商品数量--
    reduceNum() {
        let num = this.data.num
        if (num > 1) {
            this.data.num--
        } else {
            this.data.num == 1
            wx.showToast({
                title: '不能再减啦!',
                icon: 'error'
            })
            return
        }
        this.setData({
            num: this.data.num
        })
    },
    // 商品数量++
    addNum() {
        let num = this.data.num
        if (num >= 1) {
            this.data.num++
        }
        this.setData({
            num: this.data.num
        })
    },
    // 加入购物车
    addCart(e) {
        // 查询是否登录 没有登录跳转到登录页面
        console.log(app.globalData.token);
        if (app.globalData.token !== '') {
            let pid = e.currentTarget.dataset.pid
            // 获取count
            let count = this.data.num
            // 获取rule
            let trule = this.data.temData[this.data.tindex]
            let srule = this.data.sugarData[this.data.sindex]
            let crule = this.data.creamData[this.data.cindex]
            let ruleList = [];
            ruleList.push(trule, srule, crule)
            console.log(ruleList);
            let arr = []
            for (let i = 0; i <= ruleList.length - 1; i++) {
                if (ruleList[i].trim() != "") {
                    arr.push(ruleList[i])
                }
            }
            wx.request({
                url: 'http://www.kangliuyong.com:10002/addShopcart',
                method: 'POST',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },
                data: {
                    pid: pid,
                    appkey: app.globalData.appkey,
                    count: count,
                    rule: arr,
                    tokenString: app.globalData.token
                },
                success: (res) => {
                    console.log(res);
                    if (res.data.status == 1) {
                        wx.showToast({
                            title: '加入购物车成功',
                        })
                    } else if (res.data.status == 0) {
                        wx.showToast({
                            title: '已经加入购物车啦',
                            icon: 'error'
                        })
                    } else {
                        wx.showToast({
                            title: '加入购物车失败',
                            icon: 'error'
                        })
                    }
                }
            })
        } else {
            wx.showModal({
                title: '无法加入购物车',
                content: '请先登录再操作',
                success: (res) => {
                    if (res.confirm) {
                        console.log('用户登录');
                        wx.navigateTo({
                            url: '/pages/login/login',
                        })
                    } else if (res.cancel) {
                        console.log('用户取消');
                    }
                }
            })
        }
    },
    //立即购买
    settlement(e) {
        if (app.globalData.token !== '') {
            // 将数据加入购物车
            // 获取pid
            let pid = e.currentTarget.dataset.pid
            console.log(pid);
            // 获取count
            let count = this.data.num
            console.log(count);
            // 获取rule
            let trule = this.data.temData[this.data.tindex]
            let srule = this.data.sugarData[this.data.sindex]
            let crule = this.data.creamData[this.data.cindex]
            let ruleList = [];
            ruleList.push(trule, srule, crule)
            console.log(ruleList);
            let arr = []
            for (let i = 0; i <= ruleList.length - 1; i++) {
                if (ruleList[i].trim() != "") {
                    arr.push(ruleList[i])
                }
            }
            wx.request({
                url: 'http://www.kangliuyong.com:10002/addShopcart',
                method: 'POST',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },
                data: {
                    pid: pid,
                    appkey: app.globalData.appkey,
                    count: count,
                    rule: arr,
                    tokenString: app.globalData.token
                },
                success: (res) => {
                    console.log(res);
                    let sidList = []
                    sidList.push(res.data.sid)
                    console.log(sidList);
                    if (res.data.msg == '加入购物车成功') {
                        this.setData({
                            sidData: sidList
                        })
                        let sid = JSON.stringify(this.data.sidData)
                        console.log(sid);
                        wx.navigateTo({
                            url: '/pages/order/order?sid=' + sid,
                        })
                    }
                }
            })
        } else {
            wx.showModal({
                title: '无法购买',
                content: '请先登录再操作',
                success: (res) => {
                    if (res.confirm) {
                        console.log('用户登录');
                        wx.navigateTo({
                            url: '/pages/login/login',
                        })
                    } else if (res.cancel) {
                        console.log('用户取消');
                    }
                }
            })
        }
    },
    // 收藏商品
    collection(e) {
        let pid = e.currentTarget.dataset.pid
        if (app.globalData.token !== '') {
            wx.request({
                url: 'http://www.kangliuyong.com:10002/findlike',
                data: {
                    appkey: app.globalData.appkey,
                    pid: pid,
                    tokenString: app.globalData.token
                },
                success: res => {
                    console.log(res);
                    let love = this.data.love
                    love = !love
                    console.log(love);
                    this.setData({
                        love: love
                    })
                    if (love == true) {
                        wx.request({
                            url: 'http://www.kangliuyong.com:10002/like',
                            method: "POST",
                            header: {
                                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                            },
                            data: {
                                appkey: app.globalData.appkey,
                                pid: pid,
                                tokenString: app.globalData.token
                            },
                            success: res => {
                                console.log(res);
                                if (res.data.msg == "已收藏") {
                                    wx.showToast({
                                        title: '收藏成功',
                                    })
                                }
                            }
                        })
                    } else {
                        wx.request({
                            url: 'http://www.kangliuyong.com:10002/notlike',
                            method: "POST",
                            header: {
                                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                            },
                            data: {
                                appkey: app.globalData.appkey,
                                pid: pid,
                                tokenString: app.globalData.token
                            },
                            success: res => {
                                console.log(res);
                                if (res.data.msg == "已取消收藏") {
                                    wx.showToast({
                                        title: '取消收藏',
                                        icon: 'error'
                                    })
                                }
                            }
                        })
                    }
                }
            })
        } else {
            wx.showModal({
                title: '无法收藏商品',
                content: '请先登录再操作',
                success: (res) => {
                    if (res.confirm) {
                        console.log('用户登录');
                        wx.navigateTo({
                            url: '/pages/login/login',
                        })
                    } else if (res.cancel) {
                        console.log('用户取消');
                    }
                }
            })
        }

    },
    // 跳转购物车页面
    pushShopcar() {
        wx.switchTab({
            url: '/pages/shopcar/shopcar',
        })
    }
})